﻿/*
 * Created by SharpDevelop.
 * User: user
 * Date: 21.09.2012
 * Time: 14:30
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace ServicesPayments
{
	/// <summary>
	/// Description of CatalogueFormBase.
	/// </summary>
	public partial class frmCatalogueTemplate : Form
	{

        public IList DataSource
        {
            get { return (IList)dataGridView1.DataSource; }
            set { dataGridView1.DataSource = value; }
        }
		
		public frmCatalogueTemplate()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//

		    this.dataGridView1.AutoGenerateColumns = false;
		    var i = 0;
		}	
		
		public void ProcessGridViewMetadata(Type catalogEntryType)
		{
			if (catalogEntryType.BaseType == typeof(CatalogEntryBase))
			{
				foreach (var property in catalogEntryType.GetProperties())
				{
					//var gcattr = property.GetCustomAttribute(typeof (GridColumnAttribute)) as GridColumnAttribute;
				    foreach (var gcattr in property.GetCustomAttributes(false).OfType<GridColumnAttribute>())
				    {
				        //int index=
                            dataGridView1.Columns.Add(new DataGridViewColumn
				                                      {
                                                          CellTemplate = property.PropertyType == typeof (bool)
				                                                        ? (DataGridViewCell) new DataGridViewCheckBoxCell()
				                                                        : new DataGridViewTextBoxCell(),
				                                          HeaderText = gcattr.Caption,
				                                          DataPropertyName = property.Name,
				                                          DisplayIndex = gcattr.ColumnIndex,
				                                          Visible = gcattr.Visible

				                                      });
				        //dataGridView1.Columns[index].CellTemplate = ;
				    }
				}
			}
		}

       

	}
}
